package com.ruoyi.book.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.book.domain.Author;
import com.ruoyi.book.service.IAuthorService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 作者Controller
 *
 * @author laoliu
 * @date 2023-05-06
 */
@RestController
@RequestMapping("/book/author")
public class AuthorController extends BaseController {
    @Value("${book.author-role}")
    private String authorRole = "ZUOJIA";//AUTHOR
    @Autowired
    private IAuthorService authorService;

    @Autowired
    private ISysUserService userService;

//    @Autowired
//    private ISysUserService sysUserService;

    /**
     * 查询作者列表
     */
    @PreAuthorize("@ss.hasPermi('book:author:list')")
    @GetMapping("/list")
    public TableDataInfo list(Author author) {
        startPage();

        LoginUser loginUser = SecurityUtils.getLoginUser();
        List<SysRole> roles = loginUser.getUser().getRoles();
        boolean collect = roles.stream().anyMatch(x -> {
            return x.getRoleKey().equals(authorRole);
        });
        //user_id 找到author...

        if(collect){//待完善
            author.setUserId(loginUser.getUserId());
        }
        List<Author> list = authorService.selectAuthorList(author);
        return getDataTable(list);
    }

    /**
     * 导出作者列表
     */
    @PreAuthorize("@ss.hasPermi('book:author:export')")
    @Log(title = "作者", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Author author) {
        List<Author> list = authorService.selectAuthorList(author);
        ExcelUtil<Author> util = new ExcelUtil<Author>(Author.class);
        util.exportExcel(response, list, "作者数据");
    }

    /**
     * 获取作者详细信息
     */
    @PreAuthorize("@ss.hasPermi('book:author:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(authorService.selectAuthorById(id));
    }


    /**
     * 获取作者对应的系统用户信息
     */
    @PreAuthorize("@ss.hasPermi('book:author:query')")
    @GetMapping(value = "/sys/{id}")
    public AjaxResult getRelateSysUser(@PathVariable("id") Long id) {

        SysUser user = this.userService.selectUserById(id);
        AjaxResult ajax = AjaxResult.success(user);
        ajax.put("roleGroup", userService.selectUserRoleGroup(user.getUserName()));
        ajax.put("postGroup", userService.selectUserPostGroup(user.getUserName()));
        return ajax;
    }

    /**
     * 新增作者
     */
    @PreAuthorize("@ss.hasPermi('book:author:add')")
    @Log(title = "作者", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Author author) {
        return toAjax(authorService.insertAuthor(author));
    }

    //查找当前用户
    @PreAuthorize("@ss.hasPermi('book:author:add')")
    @Log(title = "作者", businessType = BusinessType.INSERT)
    @GetMapping("showSysuser")
    public AjaxResult addShowSysuser(SysUser user) {
        //LoginUser loginUser = SecurityUtils.getLoginUser();
        //loginUser.getUser().getRoles()
        //user.setPhonenumber(loginUser.getUser().getPhonenumber());
        user.getParams().put("role", this.authorRole);
        List<SysUser> list = this.authorService.selectSysUserList(user);
        return AjaxResult.success(list);
    }

    /**
     * 修改作者
     */
    @PreAuthorize("@ss.hasPermi('book:author:edit')")
    @Log(title = "作者", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Author author) {
        return toAjax(authorService.updateAuthor(author));
    }

    /**
     * 删除作者
     */
    @PreAuthorize("@ss.hasPermi('book:author:remove')")
    @Log(title = "作者", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(authorService.deleteAuthorByIds(ids));
    }
}
